
package com.rt.schedulenew.ws;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rt.schedulebase.dto.*;
import com.rt.schedulebase.entity.DictWorkDate;
import com.rt.schedulebase.entity.ExamApply;
import com.rt.schedulebase.entity.ExamApplyOrgan;
import com.rt.schedulebase.entity.ExamScheduleItem;
import com.rt.schedulebase.mapper.ExamApplyItemMapper;
import com.rt.schedulebase.mapper.ExamApplyMapper;
import com.rt.schedulebase.mapper.ExamScheduleItemMapper;
import com.rt.schedulenew.api.*;
import com.rt.schedulenew.dto.ResponseDto;
import com.rt.schedulenew.dto.WaitingDto;
import com.rt.schedulenew.service.impl.CommonServiceImpl;
import com.rt.schedulenew.service.impl.ConfigServiceImpl;
import com.rt.schedulenew.utils.annotation.ServiceLimit;
import com.rt.schedulenew.utils.base.CommonUtil;
import com.rt.schedulenew.utils.base.ConstantsUtil;
import com.rt.schedulenew.utils.base.XmlUtil;
import com.rt.schedulenew.utils.global.BusinessException;
import com.rt.schedulenew.utils.global.JsonResult;
import com.rt.schedulenew.utils.global.LocalLock;
import com.rt.schedulenew.utils.util.*;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.jws.WebService;
import java.lang.reflect.Method;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

@WebService(serviceName = "scheduleService",
        targetNamespace = "http://webservice.gxinfo.com",
        endpointInterface = "com.rt.schedulenew.ws.IScheduleService")
@Component
public class ScheduleServiceImpl implements IScheduleService {
    private Logger log = LoggerFactory.getLogger(ScheduleServiceImpl.class);

    @Autowired
    private MarshallerUtils marshallerUtils;
    @Autowired
    private ExamApplyMapper examApplyMapper;
    @Autowired
    private ExamApplyItemMapper examApplyItemMapper;
    @Autowired
    private IExamApplyService examApplyService;
    @Autowired
    private IDictHospitalService dictHospitalService;
    @Autowired
    private IExamApplyPatientService examApplyPatientService;
    @Autowired
    private IExamScheduleService examScheduleService;
    @Autowired
    private ICommonService commonService;
    @Autowired
    private IDictTerminalService dictTerminalService;
    @Autowired
    private IDictExamClassService dictExamClassService;
    @Autowired
    private IDictWorkDateService dictWorkDateService;
    @Autowired
    private ConfigServiceImpl configService;
    @Autowired
    private IScheduleSignService scheduleSignService;
    @Autowired
    private IDictExamQueueService dictExamQueueService;
    @Autowired
    private ExamScheduleItemMapper examScheduleItemMapper;
    @Autowired
    private IExamScheduleActService examScheduleActService;
    @Autowired
    private IExamApplyOrganService examApplyOrganService;

    @Override
    public String GetScheduleDates(String req) {
        return getScheduleDates(req, "");
    }

    /***
     * 获取院区列表
     * @param req
     * @return
     */
    @Override
    public String GetHospitals(String req) {
        return getHospitals(req, "");
    }

    /**
     * 获取院区详细信息 hospitalCode,terminalType
     *
     * @param req
     * @return
     */
    @Override
    public String GetHospitalInfo(String req) {
        return getHospitalInfo(req, "");
    }

    /*
    获取患者信息
     */
    @Override
    public String GetPatientInfo(String req) {
        return getPatientInfo(req, ConstantsUtil.TerminalType.SELF.getType());
    }

    /**
     * 获取患者检查申请单列表信息
     *
     * @param req
     * @return
     */
    @Override
    public String GetPatientApplys(String req) {
        return getPatientApplys(req, "");
    }

    @Override
    public String GetSchedulePlans(String req) {
        return getSchedulePlans(req, "");
    }

    @Override
    public String GetExamApplyInfo(String req) {
        return getExamApplyInfo(req, "");
    }

    @Override
    public String GetEnabledScheduleApms(String req) {
        return getEnabledScheduleApms(req, "");
    }

    @Override
    public String IsGestational(String req) {
        return isGestational(req, "");
    }

    @Override
    public String EditLastMensesDate(String req) {
        return editLastMensesDate(req, "");
    }

    @LocalLock(key = "localLock:arg[0]")
    @ServiceLimit(limitType = ServiceLimit.LimitType.URL)
    @Override
    public String SetScheduleApm(String req) {
        return setScheduleApm(req, "");
    }

    @LocalLock(key = "localLock:arg[0]")
    @Override
    public String DelScheduleApm(String req) {
        return delScheduleApm(req, "");
    }

    @Override
    public String EditScheduleStatus(String req) {
        return editScheduleStatus(req, "");
    }

    @Override
    public String ConfirmScheduleApm(String req) {
        return confirmScheduleApm(req, "");
    }

    @LocalLock(key = "localLock:arg[0]")
    @Override
    public String EditScheduleApm(String req) {
        return editScheduleApm(req, "");
    }

    @Override
    public String GetLastMensesDate(String req) {
        return getLastMensesDate(req, "");
    }

    @Override
    public String SignInScheduled(String req) {
        return signInScheduled(req, "");
    }

    @Override
    public String SignInScheduledMulti(String req) {
        return signInScheduledMulti(req, "");
    }

    @Override
    public String GetScheduleSigned(String req) {
        return getScheduleSigned(req, "");
    }

    @Override
    public String GetSelfDeviceInfo(String req) {
        String reqStr = req.replace("deviceId", "ipAddress");

        DictTerminalDto dto = XmlUtil.xmlRequest2Bean(reqStr, DictTerminalDto.class, "ipAddress");
        if (StringUtils.isBlank(dto.getIpAddress())) {
            return XmlUtil.object2xmlResponse("1", "设备ID不能为空！");
        }
        DictTerminalDto resultDto = dictTerminalService.getSelfDeviceInfo(dto);
        if (resultDto == null) {
            return XmlUtil.object2xmlResponse("1", "自助机配置信息不存在！");
        }
        return XmlUtil.object2xmlResponse("SelfDevice", resultDto, "0", "成功");
    }

    @Override
    public String GetEnabledSignClasses(String req) {
        return getEnabledSignClasses(req, "");
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public String EditPrintFlag(String req) {
        return editPrintFlag(req, "");
    }

    @Override
    public String GetScheduleExpireDate(String req) {
        return getScheduleExpireDate(req, "");
    }

    @Override
    public String GetExamNotices(String req) {
        return getExamNotices(req, "");
    }

    @Override
    public String Print(String req) {
        ExamApplyDto dto = XmlUtil.xmlRequest2Bean(req, ExamApplyDto.class, "applyNo,mergeNo,terminalType,deviceId,userId,userName");
        if (StringUtils.isBlank(dto.getMergeNo())) {
            ExamApplyItemDto examApplyItemDto = new ExamApplyItemDto();
            examApplyItemDto.setMergeNos(CommonUtil.split2List(dto.getApplyNo()));
            String mergeNo = getMergeNos(examApplyItemDto);
            dto.setMergeNo(mergeNo);
        }
        JsonResult jsonResult = examApplyService.print(dto);
        if (jsonResult != null && jsonResult.getResult() != null) {
            return XmlUtil.object2xmlResponse("PrintPattern", jsonResult.getResult(), jsonResult.getStatus(), jsonResult.getMessage());
        }
        return XmlUtil.object2xmlResponse("1", "没获取到对应的打印模版，请联系管理员设置自助机打印模版");
    }

    @Override
    public String EditFlag(String req) {
        ExamApplyDto dto = XmlUtil.xmlRequest2Bean(req, ExamApplyDto.class, "applyNo,mergeNo,type,flag,terminalType,deviceId,userId,userName");
        if (ConstantsUtil.TerminalType.SELF.getType().equals(dto.getTerminalType()) && StringUtils.isNotBlank(dto.getType()) && StringUtils.isNotBlank(dto.getFlag()) && "12".equals(dto.getType()) && "0".equals(dto.getFlag())) {
            dto.setFlag("2");
        }
        JsonResult jsonResult = examApplyService.editApplyFlag(dto);
        if (jsonResult != null) {
            return XmlUtil.object2xmlResponse(jsonResult.getStatus(), jsonResult.getMessage());
        }
        return XmlUtil.object2xmlResponse("1", "调更新申请单标识接口失败");
    }

    @Override
    public String EditExamApply(String req) {
        ExamApplyDto dto = XmlUtil.xmlRequest2Bean(req, ExamApplyDto.class, "icCard,sickId,applyNo,phoneNumber,terminalType,deviceId,userId,userName,commonCard");
        if (StringUtils.isBlank(dto.getSickId()) && StringUtils.isBlank(dto.getIcCard())) {
            return XmlUtil.object2xmlResponse("1", "病人ID和IC卡号不能同时为空");
        }
        if (StringUtils.isBlank(dto.getSickId())) {
            if (StringUtils.isNotBlank(dto.getTerminalType()) && ConstantsUtil.TerminalType.SELF.getType().equals(dto.getTerminalType()) && StringUtils.isNotBlank(dto.getDeviceId())) {
                DictTerminalDto dictTerminalDto = new DictTerminalDto();
                dictTerminalDto.setIpAddress(dto.getDeviceId());
                DictTerminalDto terminalDto = dictTerminalService.getSelfDeviceInfo(dictTerminalDto);
                if (terminalDto == null) {
                    return XmlUtil.object2xmlResponse("1", "自助机配置信息不存在");
                }
                convertCardType4Self(dto, terminalDto);
            } else {
                if (StringUtils.isBlank(dto.getCommonCard())) {
                    dto.setCommonCard(dto.getIcCard());
                }
                dto.setIcCard((String) null);
                dto.setSickId((String) null);
            }
            List<String> sickIds = commonService.getSickIds(dto);
            if (sickIds.size() == 0) {
                return XmlUtil.object2xmlResponse("1", "没有您的检查申请记录,如有疑问请到护士站咨询");
            }
            dto.setSickId(StringUtils.join((Iterable) sickIds, ","));
        }
        JsonResult jsonResult = examApplyService.editExamApply(dto);
        if (jsonResult != null) {
            return XmlUtil.object2xmlResponse(jsonResult.getStatus(), jsonResult.getMessage());
        }
        return XmlUtil.object2xmlResponse("1", "调修改申请单信息接口失败");
    }

    @Override
    public String GetFirstScheduleApm(String req) {
        ExamApplyDto dto = XmlUtil.xmlRequest2Bean(req, ExamApplyDto.class, "applyNo,hospitalCode,mergeNo,terminalType,deviceId,userId,userName");
        if (StringUtils.isBlank(dto.getMergeNo())) {
            ExamApplyItemDto examApplyItemDto = new ExamApplyItemDto();
            examApplyItemDto.setMergeNos(CommonUtil.split2List(dto.getApplyNo()));
            String mergeNo = getMergeNos(examApplyItemDto);
            dto.setMergeNo(mergeNo);
        }
        JsonResult jsonResult = scheduleSignService.getDateFirst(dto);
        if (jsonResult == null) {
            return XmlUtil.object2xmlResponse("1", "调获取最早可预约日期接口失败");
        }
        if ("0".equals(jsonResult.getStatus()) && jsonResult.getResult() != null) {
            List<ExamApplyDto> scheduleApms = new ArrayList<>();
            ExamApplyDto scheduleApm = new ExamApplyDto();
            Map<String, String> map = (Map<String, String>) jsonResult.getResult();
            if (map.containsKey("scheduleDate")) {
                String firstScheduleDate = map.get("scheduleDate");
                String firstScheduleApm = map.get("scheduleApm");
                scheduleApm.setScheduleDate(firstScheduleDate);
                scheduleApm.setScheduleApm(firstScheduleApm);
                scheduleApm.setEnabled("1");
            } else {
                scheduleApm.setEnabled("0");
            }
            scheduleApms.add(scheduleApm);
            return XmlUtil.object2xmlResponse("ScheduleApm", scheduleApms, jsonResult.getStatus(), jsonResult.getMessage());
        }
        return XmlUtil.object2xmlResponse("1", StringUtils.isNotBlank(jsonResult.getMessage()) ? jsonResult.getMessage() : "获取最早可预约日期失败");
    }

    @Override
    public String SignIn(String req) {
        ExamApplyDto dto = XmlUtil.xmlRequest2Bean(req, ExamApplyDto.class, "applyNo,terminalType,deviceId,userId,userName,mergeNo,application,commonCard");
        if (ConstantsUtil.TerminalType.SELF.getType().equals(dto.getTerminalType())) {
            DictTerminalDto dictTerminalDto = new DictTerminalDto();
            dictTerminalDto.setIpAddress(dto.getDeviceId());
            DictTerminalDto terminalDto = dictTerminalService.getSelfDeviceInfo(dictTerminalDto);
            if (terminalDto == null || StringUtils.isBlank(terminalDto.getSignExamClass())) {
                return XmlUtil.object2xmlResponse("1", "本机不能办理检查报到，如有疑问请到护士站咨询！");
            }
            convertCardType4Self(dto, terminalDto);
            dto.setScheduledDate(DateUtil.getCurrDateStr());
            dto.setUnSignQueues(CommonUtil.split2List(terminalDto.getUnSignQueue()));
            dto.setExamClasses(CommonUtil.split2List(terminalDto.getSignExamClass()));
            dto.setScheduleHospital(terminalDto.getHospitalCode());
            List<ExamApplyDto> list = examApplyMapper.findByApplyNo(dto);
            if (list == null || list.size() == 0) {
                String strReturn = "本机仅提供" + terminalDto.getSignExamClass() + "检查报到，如有疑问请到护士站咨询！";
                String reminder = configService.customReminder(ConstantsUtil.ConfigName.NO_SIGN_QUEUE_BY_SELF_REMINDER.getKey(), "", terminalDto.getSignExamClass());
                if (StringUtils.isNotBlank(reminder)) {
                    strReturn = reminder;
                }
                return XmlUtil.object2xmlResponse("1", strReturn);
            }
        }
        if (StringUtils.isBlank(dto.getMergeNo())) {
            List<ExamApplyDto> list2 = examApplyMapper.findByApplyNo(dto);
            StringBuffer applyNos = new StringBuffer();
            for (ExamApplyDto examApplyDto : list2) {
                if (list2.get(0).getScheduledId().equals(examApplyDto.getScheduledId())) {
                    applyNos.append(examApplyDto.getApplyNo() + "-" + examApplyDto.getItemNo());
                }
            }
            dto.setMergeNo(applyNos.substring(1));
        }
        List<Object> responseDtoList = new ArrayList<Object>();
        JsonResult jsonResult = examApplyService.signinSchedule(dto);
        if ("0".equals(jsonResult.getStatus())) {
            ExamApplyDto ea = (ExamApplyDto) jsonResult.getResult();
            responseDtoList = resultToSignInNodeV2(ea, dto.getTerminalType(), dto.getApplication());
            for (Object obj : responseDtoList) {
                ResponseDto responseDto = (ResponseDto) obj;
                responseDto.setMsgCode(jsonResult.getStatus());
                responseDto.setMsgDesc(jsonResult.getMessage());
            }
            if (StringUtils.isBlank(ea.getIsSign())) {
                ExamApplyDto examApplyDto = new ExamApplyDto();
                List<String> applyNos2 = CommonUtil.mergeNo2ApplyNos(dto.getMergeNo());
                examApplyDto.setApplyNos(applyNos2);
                String actType = ConstantsUtil.ActType.SIGN.getType();
                examApplyDto.setActType(actType);
                ExamApplyDto applyResult = (ExamApplyDto) jsonResult.getResult();
                examApplyDto.setExamClass(applyResult.getExamClass());
                examApplyDto.setScheduledId(applyResult.getScheduledId());
                examApplyDto.setMergeNo(applyResult.getMergeNo());
                examApplyDto.setApplyNo(dto.getApplyNo());
                configService.isScheduleSync(examApplyDto, actType, ConstantsUtil.ScheduleStatus.TEN.getStatus());
            }
        } else {
            ResponseDto responseDto2 = new ResponseDto();
            responseDto2.setMsgCode(jsonResult.getStatus());
            responseDto2.setMsgDesc(jsonResult.getMessage());
            responseDtoList.add(responseDto2);
        }
        return XmlUtil.object2xmlResponse_V2("Message", responseDtoList, "0", "成功");
    }

    /**
     * 批量报到
     *
     * @param req
     * @return
     */
    @Override
    public String SignInMulti(String req) {
        ExamApplyDto dto = XmlUtil.xmlRequest2Bean(req, ExamApplyDto.class, "sickId,icCard,examClass,terminalType,deviceId,application,userId,userName,commonCard");
        DictTerminalDto dictTerminalDto = new DictTerminalDto();
        dictTerminalDto.setIpAddress(dto.getDeviceId());
        DictTerminalDto terminalDto = dictTerminalService.getSelfDeviceInfo(dictTerminalDto);
        if (terminalDto == null || StringUtils.isBlank(terminalDto.getSignExamClass())) {
            return XmlUtil.object2xmlResponse("1", "本机不能办理检查报到，如有疑问请到护士站咨询！");
        }
        convertCardType4Self(dto, terminalDto);
        List<String> sickIds = null;
        if (StringUtils.isNotBlank(dto.getSickId())) {
            sickIds = CommonUtil.split2List(dto.getSickId());
        } else {
            sickIds = commonService.getSickIds(dto);
        }
        if (sickIds.size() == 0) {
            String strReturn = "您的检查申请单信息不存在，如有疑问请到护士站咨询！";
            String reminder = configService.customReminder(ConstantsUtil.ConfigName.NO_QUERY_EXAM_APPLY_REMINDER.getKey(), "", "");
            if (StringUtils.isNotBlank(reminder)) {
                strReturn = reminder;
            }
            return XmlUtil.object2xmlResponse("1", strReturn);
        }
        dto.setScheduledDate(DateUtil.getCurrDateStr());
        dto.setSickIds(sickIds);
        dto.setUnSignQueues(CommonUtil.split2List(terminalDto.getUnSignQueue()));
        dto.setExamClasses(CommonUtil.split2List(terminalDto.getSignExamClass()));
        dto.setScheduleHospital(terminalDto.getHospitalCode());
        List<ExamApplyDto> list = examApplyMapper.findByApplyNo(dto);
        List<Object> responseDtoList = new ArrayList<Object>();
        List<String> tmpList = new ArrayList<String>();
        if (list == null || list.size() == 0) {
            String strReturn2 = "本机仅提供" + terminalDto.getSignExamClass() + "检查报到，如有疑问请到护士站咨询！";
            String reminder2 = configService.customReminder(ConstantsUtil.ConfigName.NO_SIGN_QUEUE_BY_SELF_REMINDER.getKey(), "", terminalDto.getSignExamClass());
            if (StringUtils.isNotBlank(reminder2)) {
                strReturn2 = reminder2;
            }
            return XmlUtil.object2xmlResponse("1", strReturn2);
        }
        list.removeIf(eaDto -> "01".equals(eaDto.getScheduleStatus()));
        if (ConstantsUtil.TerminalType.SELF.getType().equals(dto.getTerminalType()) && StringUtils.isNotBlank(dto.getApplication())) {
            list.removeIf(eaDto -> StringUtils.isNotBlank(eaDto.getExamStatus()) && Integer.parseInt(eaDto.getExamStatus()) >= 10);
        }
        for (ExamApplyDto examApply : list) {
            if (!tmpList.contains(examApply.getScheduledId())) {
                List<String> repeatSignByExamClassList = configService.getConfigListByName(ConstantsUtil.ConfigName.REPEAT_SIGN_BY_EXAM_CLASS.getKey());
                if (ConstantsUtil.TerminalType.SELF.getType().equals(dto.getTerminalType()) && StringUtils.isNotBlank(examApply.getExamStatus()) && Integer.parseInt(examApply.getExamStatus()) >= 10 && (repeatSignByExamClassList.isEmpty() || !repeatSignByExamClassList.contains(examApply.getExamClass()))) {
                    ResponseDto responseDto = resultToNode(examApply, dto.getTerminalType(), dto.getApplication());
                    String msgDesc = "";
                    if (StringUtils.isBlank(dto.getApplication())) {
                        msgDesc = "您今天的" + examApply.getExamClass() + "检查预约已经报到了，<br/>【已报到信息】<br/>排队号：" + responseDto.getQueueNo() + "<br/>预约时间：" + responseDto.getScheduleDate() + "<br/>预约时段：" + responseDto.getScheduleApm() + "<br/>检查队列：" + responseDto.getQueueName() + "<br/>检查地点：" + responseDto.getLocation();
                    } else {
                        msgDesc = "您今天的" + examApply.getExamClass() + "检查预约已经报到了，【已报到信息】排队号：" + responseDto.getQueueNo() + "，预约时间：" + responseDto.getScheduleDate() + "，预约时段：" + responseDto.getScheduleApm() + "，检查队列：" + responseDto.getQueueName() + "，检查地点：" + responseDto.getLocation();
                    }
                    ResponseDto newResponseDto = new ResponseDto();
                    newResponseDto.setMsgCode("1");
                    newResponseDto.setMsgDesc(msgDesc);
                    responseDtoList.add(newResponseDto);
                } else {
                    ExamApplyDto applyDto = new ExamApplyDto();
                    applyDto.setApplyNo(examApply.getApplyNo());
                    applyDto.setTerminalType(dto.getTerminalType());
                    applyDto.setUserId(dto.getUserId());
                    applyDto.setUserName(dto.getUserName());
                    if (StringUtils.isNotBlank(examApply.getScheduleDate()) && DateUtil.isToday(examApply.getScheduleDate())) {
                        StringBuffer applyNos = new StringBuffer();
                        for (ExamApplyDto apply : list) {
                            if (examApply.getScheduledId().equals(apply.getScheduledId())) {
                                applyNos.append("," + apply.getApplyNo() + "-" + apply.getItemNo());
                            }
                        }
                        applyDto.setMergeNo(applyNos.substring(1));
                        JsonResult jsonResult = examApplyService.signinSchedule(applyDto);
                        if ("0".equals(jsonResult.getStatus())) {
                            ExamApplyDto applyResult = (ExamApplyDto) jsonResult.getResult();
                            List<Object> resultList = resultToSignInNodeV2(applyResult, dto.getTerminalType(), dto.getApplication());
                            for (Object obj : resultList) {
                                ResponseDto responseDto2 = (ResponseDto) obj;
                                responseDto2.setMsgCode(jsonResult.getStatus());
                                responseDto2.setMsgDesc(jsonResult.getMessage());
                                responseDtoList.add(responseDto2);
                            }
                            if (StringUtils.isBlank(applyResult.getIsSign())) {
                                ExamApplyDto examApplyDto = new ExamApplyDto();
                                List<String> applyNoList = (List<String>) CommonUtil.mergeNo2ApplyNos(applyDto.getMergeNo());
                                examApplyDto.setApplyNos((List) applyNoList);
                                String actType = ConstantsUtil.ActType.SIGN.getType();
                                examApplyDto.setActType(actType);
                                examApplyDto.setExamClass(applyResult.getExamClass());
                                examApplyDto.setScheduledId(applyResult.getScheduledId());
                                examApplyDto.setMergeNo(applyResult.getMergeNo());
                                examApplyDto.setApplyNo(applyDto.getApplyNo());
                                configService.isScheduleSync(examApplyDto, actType, ConstantsUtil.ScheduleStatus.TEN.getStatus());
                            }
                        } else {
                            ResponseDto responseDto3 = new ResponseDto();
                            responseDto3.setMsgCode(jsonResult.getStatus());
                            responseDto3.setMsgDesc(jsonResult.getMessage());
                            responseDtoList.add(responseDto3);
                        }
                    }
                }
                tmpList.add(examApply.getScheduledId());
            }
        }
        return XmlUtil.object2xmlResponse_V2("Message", responseDtoList, "0", "成功");
    }

    private List<Object> resultToSignInNodeV2(ExamApplyDto dto, String terminalType, String application) {
        List<Object> messageResultList = new ArrayList<Object>();
        ResponseDto responseDto = resultToNode(dto, terminalType, application);
        List<String> examNoList = responseDto.getExamNos();
        for (String examNo : examNoList) {
            List<ResponseDto> examApplyList = new ArrayList<ResponseDto>();
            Map<String, List<ResponseDto>> examApplyMap = new HashMap<String, List<ResponseDto>>();
            for (ExamItemDto itemDto : responseDto.getExamItem()) {
                if (examNo.equals(itemDto.getExamNo())) {
                    if (!examApplyMap.containsKey(itemDto.getApplyNo())) {
                        ResponseDto examApply = new ResponseDto();
                        examApply.setApplyNo(itemDto.getApplyNo());
                        examApply.setReqDate(StringUtils.isNotBlank(itemDto.getReqDate()) ? itemDto.getReqDate() : responseDto.getReqDate());
                        examApply.setReqTime(StringUtils.isNotBlank(itemDto.getReqTime()) ? itemDto.getReqTime() : responseDto.getReqTime());
                        examApply.setApplyId(responseDto.getApplyId());
                        List<ExamItemDto> itemDtoList = new ArrayList<ExamItemDto>();
                        itemDtoList.add(itemDto);
                        examApply.setExamItem((List) itemDtoList);
                        examApply.setReminder(responseDto.getReminder());
                        if (StringUtils.isNotBlank(itemDto.getNotice())) {
                            List<String> notices = new ArrayList<String>();
                            notices.add(itemDto.getNotice());
                            notices = (List<String>) CommonUtil.convertNotice((List) notices, responseDto.getPatientSource(), responseDto.getEndTime(), responseDto.getBirthDate());
                            examApply.setNotice((List) notices);
                        }
                        List<ExamApplyOrgan> examApplyOrganList = new ArrayList<ExamApplyOrgan>();
                        if (responseDto.getExamOrgan() != null && !responseDto.getExamOrgan().isEmpty()) {
                            for (ExamApplyOrgan examApplyOrgan : responseDto.getExamOrgan()) {
                                if (StringUtils.isNotBlank(examApplyOrgan.getApplyNo()) && itemDto.getItemNo() != null && examApplyOrgan.getItemNo() != null && itemDto.getApplyNo().equals(examApplyOrgan.getApplyNo()) && itemDto.getItemNo().equals(examApplyOrgan.getItemNo())) {
                                    examApplyOrganList.add(examApplyOrgan);
                                }
                            }
                        }
                        examApply.setExamOrgan((List) examApplyOrganList);
                        examApplyList.add(examApply);
                    } else {
                        examApplyList = examApplyMap.get(itemDto.getApplyNo());
                        for (ResponseDto examApplyChild : examApplyList) {
                            if (examApplyChild.getApplyNo().equals(itemDto.getApplyNo())) {
                                examApplyChild.getExamItem().add(itemDto);
                                if (StringUtils.isNotBlank(itemDto.getNotice())) {
                                    examApplyChild.getNotice().add(itemDto.getNotice());
                                    List<String> oldNotice = (List<String>) CommonUtil.convertNotice(examApplyChild.getNotice(), responseDto.getPatientSource(), responseDto.getEndTime(), responseDto.getBirthDate());
                                    examApplyChild.getNotice().clear();
                                    examApplyChild.getNotice().addAll(oldNotice);
                                }
                                if (responseDto.getExamOrgan() == null || responseDto.getExamOrgan().isEmpty()) {
                                    continue;
                                }
                                for (ExamApplyOrgan examApplyOrgan2 : responseDto.getExamOrgan()) {
                                    if (StringUtils.isNotBlank(examApplyOrgan2.getApplyNo()) && itemDto.getItemNo() != null && examApplyOrgan2.getItemNo() != null && itemDto.getApplyNo().equals(examApplyOrgan2.getApplyNo()) && itemDto.getItemNo().equals(examApplyOrgan2.getItemNo())) {
                                        examApplyChild.getExamOrgan().add(examApplyOrgan2);
                                    }
                                }
                            }
                        }
                    }
                    examApplyMap.put(itemDto.getApplyNo(), examApplyList);
                }
            }
            ResponseDto messageInfo = new ResponseDto();
            BeanUtils.copyProperties(responseDto, messageInfo);
            messageInfo.setExamNo(examNo);
            messageInfo.setExamApply((List) examApplyList);
            messageInfo.setExamItem((List) null);
            messageInfo.setExamOrgan((List) null);
            messageInfo.setExamNos((List) null);
            messageInfo.setPatLocalIds((List) null);
            messageInfo.setNotice((List) null);
            messageInfo.setReminder((List) null);
            messageInfo.setApplyNo((String) null);
            messageInfo.setApplyId((String) null);
            messageInfo.setScheduleHospital((ResponseDto.ScheduleHospital) null);
            messageResultList.add(messageInfo);
        }
        return messageResultList;
    }

    @Override
    public String GetHospitalsForSelf(String req) {
        return getHospitals(req, ConstantsUtil.TerminalType.SELF.getType());
    }

    @Override
    public String GetHospitalInfoForSelf(String req) {
        return getHospitalInfo(req, ConstantsUtil.TerminalType.SELF.getType());
    }

    @Override
    public String GetPatientInfoForSelf(String req) {
        return getPatientInfo(req, ConstantsUtil.TerminalType.SELF.getType());
    }

    @Override
    public String GetPatientApplysForSelf(String req) {
        return getPatientApplys(req, ConstantsUtil.TerminalType.SELF.getType());
    }

    @Override
    public String GetExamApplyInfoForSelf(String req) {
        return getExamApplyInfo(req, ConstantsUtil.TerminalType.SELF.getType());
    }

    @Override
    public String GetScheduleExpireDateForSelf(String req) {
        return getScheduleExpireDate(req, ConstantsUtil.TerminalType.SELF.getType());
    }

    @Override
    public String GetScheduleDatesForSelf(String req) {
        return getScheduleDates(req, ConstantsUtil.TerminalType.SELF.getType());
    }

    @Override
    public String GetEnabledScheduleApmForSelf(String req) {
        return getEnabledScheduleApms(req, ConstantsUtil.TerminalType.SELF.getType());
    }

    @Override
    public String SetScheduleApmForSelf(String req) {
        return setScheduleApm(req, ConstantsUtil.TerminalType.SELF.getType());
    }

    @Override
    public String DelScheduleApmForSelf(String req) {
        return delScheduleApm(req, ConstantsUtil.TerminalType.SELF.getType());
    }

    @Override
    public String ConfirmScheduleApmForSelf(String req) {
        return confirmScheduleApm(req, ConstantsUtil.TerminalType.SELF.getType());
    }

    @Override
    public String EditScheduleApmForSelf(String req) {
        return editScheduleApm(req, ConstantsUtil.TerminalType.SELF.getType());
    }

    @Override
    public String GetLastMensesDateForSelf(String req) {
        return getLastMensesDate(req, ConstantsUtil.TerminalType.SELF.getType());
    }

    @Override
    public String EditLastMensesDateForSelf(String req) {
        return editLastMensesDate(req, ConstantsUtil.TerminalType.SELF.getType());
    }

    @Override
    public String GetExamNoticeForSelf(String req) {
        return getExamNotices(req, ConstantsUtil.TerminalType.SELF.getType());
    }

    @Override
    public String GetEnabledClassesForSelf(String req) {
        return getEnabledSignClasses(req, ConstantsUtil.TerminalType.SELF.getType());
    }

    @Override
    public String SignInScheduledForSelf(String req) {
        return signInScheduled(req, ConstantsUtil.TerminalType.SELF.getType());
    }

    @Override
    public String SignInScheduledMultiForSelf(String req) {
        return signInScheduledMulti(req, ConstantsUtil.TerminalType.SELF.getType());
    }

    @Override
    public String IsGestationalForSelf(String req) {
        return isGestational(req, ConstantsUtil.TerminalType.SELF.getType());
    }

    @Override
    public String GetSchedulePlansForSelf(String req) {
        return getSchedulePlans(req, ConstantsUtil.TerminalType.SELF.getType());
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public String EditPrintFlagForSelf(String req) {
        return editPrintFlag(req, ConstantsUtil.TerminalType.SELF.getType());
    }

    private String getScheduleDates(String req, String terminalType) {
        ExamApplyDto dto = XmlUtil.xmlRequest2Bean(req, ExamApplyDto.class, "applyNo,startDate,endDate,hospitalCode,mergeNo,terminalType,deviceId,userId,userName,commonCard");
        if (StringUtils.isBlank(dto.getApplyNo())) {
            throw new BusinessException("applyNo不能为空");
        }
        if (StringUtils.isBlank(dto.getMergeNo())) {
            ExamApplyItemDto examApplyItemDto = new ExamApplyItemDto();
            examApplyItemDto.setMergeNos(CommonUtil.split2List(dto.getApplyNo()));
            String mergeNo = getMergeNos(examApplyItemDto);
            dto.setMergeNo(mergeNo);
        }
        if (StringUtils.isNotBlank(terminalType)) {
            dto.setTerminalType(terminalType);
        }
        if (StringUtils.isNotBlank(dto.getStartDate())) {
            dto.setStartDate(DateUtil.format(dto.getStartDate(), "yyyy-MM-dd"));
        }
        if (StringUtils.isNotBlank(dto.getEndDate())) {
            dto.setEndDate(DateUtil.format(dto.getEndDate(), "yyyy-MM-dd"));
        }
        JsonResult jsonResult = examApplyService.getDateList(dto);
        if (jsonResult != null && jsonResult.getResult() != null) {
            for (ExamApplyDto examApplyDto : (List<ExamApplyDto>) jsonResult.getResult()) {
                examApplyDto.setValue(examApplyDto.getDate());
            }
        }
        return XmlUtil.object2xmlResponse("ScheduleDate", jsonResult.getResult(), jsonResult.getStatus(), jsonResult.getMessage());
    }

    /***
     * 获取院区列表
     * @param req
     * @param terminalType
     * @return
     */
    private String getHospitals(String req, String terminalType) {
        DictHospitalDto dto = XmlUtil.xmlRequest2Bean(req, DictHospitalDto.class, "applyNo,terminalType,mergeNo,deviceId,userId,userName");
        if (StringUtils.isBlank(dto.getMergeNo()) && StringUtils.isNotBlank(dto.getApplyNo())) {
            ExamApplyItemDto examApplyItemDto = new ExamApplyItemDto();
            examApplyItemDto.setMergeNos(CommonUtil.split2List(dto.getApplyNo()));
            String mergeNo = getMergeNos(examApplyItemDto);
            dto.setMergeNo(mergeNo);
        }
        if (StringUtils.isNotBlank(terminalType)) {
            dto.setTerminalType(terminalType);
        }
        boolean regex = (StringUtils.isNotBlank(dto.getApplyNo()) || StringUtils.isNotBlank(dto.getMergeNo()))
                && (ConstantsUtil.TerminalType.SELF.getType().equals(dto.getTerminalType()) || ConstantsUtil.TerminalType.MOBILE.getType().equals(dto.getTerminalType()));
        if (regex) {
            ExamApplyDto examApplyDto = new ExamApplyDto();
            examApplyDto.setApplyNo(dto.getApplyNo());
            examApplyDto.setMergeNo(dto.getMergeNo());
            examApplyDto.setTerminalType(dto.getTerminalType());
            ExamApplyDto ea = commonService.getApplyForScheduleItem(examApplyDto);
            ea = commonService.getScheduleToExamApply(examApplyDto, ea);
            if (StringUtils.isNotBlank(ea.getExamStatus())) {
                ea.setScheduleStatus(ea.getExamStatus());
            }
            if (StringUtils.isNotBlank(ea.getDesc())) {
                return XmlUtil.object2xmlResponse("1", ea.getDesc());
            }
            String isSchedule = commonService.isScheduleByFlags(ea.getItems(), dto.getTerminalType(), ea.getChargeType());
            if (StringUtils.isNotBlank(isSchedule)) {
                return XmlUtil.object2xmlResponse("1", isSchedule);
            }
            String selfActLimit = configService.getConfigByName(ConstantsUtil.ConfigName.SELF_ACT_LIMIT.getKey());
            if (StringUtils.isNotBlank(selfActLimit) && StringUtils.isNotBlank(ea.getScheduleStatus()) && "08".equals(ea.getScheduleStatus())) {
                String isLimitSelfOperate = configService.isLimitSelfOperate(dto.getTerminalType(), ConstantsUtil.ActType.UPDATE.getType(), "", ea);
                if (StringUtils.isNotBlank(isLimitSelfOperate)) {
                    return XmlUtil.object2xmlResponse("1", isLimitSelfOperate);
                }
            }
            ea.setTerminalType(dto.getTerminalType());
            String scheduleLimit = configService.scheduleLimit2(ea);
            if (StringUtils.isNotBlank(scheduleLimit)) {
                return XmlUtil.object2xmlResponse("1", scheduleLimit);
            }
        }
        List<DictHospitalDto> hospitalList = dictHospitalService.getHospitalList(dto);
        if (CollectionUtils.isNotEmpty(hospitalList)) {
            List<DictHospitalDto> list = hospitalList;
            for (DictHospitalDto dictHospitalDto2 : list) {
                dictHospitalDto2.setCode(dictHospitalDto2.getHospitalCode());
                dictHospitalDto2.setName(dictHospitalDto2.getHospitalName());
                dictHospitalDto2.setEnabled(dictHospitalDto2.getIsEnable());
                dictHospitalDto2.setAlias(dictHospitalDto2.getHospitalAlias());
                dictHospitalDto2.setAddress(dictHospitalDto2.getMailingAddress());
            }
            return XmlUtil.object2xmlResponse("Hospital", list, "0", "成功");
        }
        return XmlUtil.object2xmlResponse("1", "没有可预约的院区");
    }

    /***
     * GetHospitalInfo
     * @param req
     * @param terminalType
     * @return
     */
    private String getHospitalInfo(String req, String terminalType) {
        DictHospitalDto dto = XmlUtil.xmlRequest2Bean(req, DictHospitalDto.class, "hospitalCode,terminalType");
        List hospitalList = dictHospitalService.getHospitalList(dto);
        return XmlUtil.object2xmlResponse("Hospital", hospitalList.get(0), "0", "成功");
    }


    /**
     * 获取患者信息
     *
     * @param req
     * @param terminalType
     * @return
     */
    private String getPatientInfo(String req, String terminalType) {
        ExamApplyPatientDto dto = XmlUtil.xmlRequest2Bean(req, ExamApplyPatientDto.class, "icCard,sickId,examClass,terminalType,deviceId,userId,userName,commonCard");
        if (StringUtils.isNotBlank(terminalType)) {
            dto.setTerminalType(terminalType);
        }
        if (StringUtils.isNotBlank(dto.getTerminalType())
                && ConstantsUtil.TerminalType.SELF.getType().equals(dto.getTerminalType())
                && StringUtils.isNotBlank(dto.getDeviceId())) {
            DictTerminalDto dictTerminalDto = new DictTerminalDto();
            dictTerminalDto.setIpAddress(dto.getDeviceId());
            DictTerminalDto terminalDto = dictTerminalService.getSelfDeviceInfo(dictTerminalDto);
            if (terminalDto == null) {
                return XmlUtil.object2xmlResponse("1", "自助机配置信息不存在");
            }
            if (StringUtils.isNotBlank(dto.getIcCard())) {
                String icCard = dto.getIcCard();
                dto.setCommonCard(icCard);
                dto.setIcCard(null);
                dto.setSickId(null);
                if (StringUtils.isNotBlank(terminalDto.getIcCardInputSupportType())) {
                    String icCardInputSupportType = terminalDto.getIcCardInputSupportType();
                    if ("common_card".equals(icCardInputSupportType)) {
                        dto.setCommonCard(icCard);
                    } else if ("sick_id".equals(icCardInputSupportType)) {
                        dto.setSickId(icCard);
                        dto.setCommonCard(null);
                    } else if ("ic_card".equals(icCardInputSupportType)) {
                        dto.setIcCard(icCard);
                        dto.setCommonCard(null);
                    } else if ("inpatient_no".equals(icCardInputSupportType)) {
                        dto.setInpatientNo(icCard);
                    } else if ("outpatient_no".equals(icCardInputSupportType)) {
                        dto.setOutpatientNo(icCard);
                    } else if ("apply_no".equals(icCardInputSupportType)) {
                        dto.setApplyNo(icCard);
                    } else if ("identity_card".equals(icCardInputSupportType)) {
                        dto.setIdentityCard(icCard);
                    } else if ("mi_card".equals(icCardInputSupportType)) {
                        dto.setMiCard(icCard);
                    } else if ("health_card".equals(icCardInputSupportType)) {
                        dto.setHealthCard(icCard);
                    }
                }
            } else {
                dto.setIcCard(null);
            }
        }
        JsonResult jsonResult = examApplyPatientService.getPatientInfo(dto);
        if (jsonResult != null) {
            if ("0".equals(jsonResult.getStatus()) && jsonResult.getResult() != null) {
                return XmlUtil.object2xmlResponse("Patient", jsonResult.getResult(), "0", jsonResult.getMessage());
            }
            if (jsonResult.getResult() == null) {
                return XmlUtil.object2xmlResponse("1", "没有查询到您的" + (StringUtils.isBlank(dto.getExamClass()) ? "" : dto.getExamClass()) + "检查申请,如有疑问请到护士站咨询!");
            }
            if (StringUtils.isNotBlank(jsonResult.getMessage())) {
                return XmlUtil.object2xmlResponse("1", jsonResult.getMessage());
            }
        }
        return XmlUtil.object2xmlResponse("1", "没有查询到您的" + (StringUtils.isBlank(dto.getExamClass()) ? "" : dto.getExamClass()) + "检查申请,如有疑问请到护士站咨询!");
    }

    /**
     * 获取患者检查申请单列表信息
     *
     * @param req
     * @param terminalType
     * @return
     */
    private String getPatientApplys(String req, String terminalType) {
        ExamApplyDto dto = XmlUtil.xmlRequest2Bean(req, ExamApplyDto.class, "sickId,icCard,examClass,reqDateStart,reqDateEnd,deptCode,patientSource,wardCode,terminalType,deviceId,planMode,applyNo,application,deviceId,userId,userName,commonCard");
        if (StringUtils.isBlank(terminalType)) {
            dto.setTerminalType(terminalType);
        }
        if (StringUtils.isBlank(dto.getPlanMode())) {
            dto.setPlanMode("0");
        }
        dto.setHistoryFlag("1");

        if (StringUtils.isNotBlank(dto.getTerminalType())
                && ConstantsUtil.TerminalType.SELF.getType().equals(dto.getTerminalType())
                && StringUtils.isNotBlank(dto.getDeviceId())) {
            DictTerminalDto dictTerminalDto = new DictTerminalDto();
            dictTerminalDto.setIpAddress(dto.getDeviceId());
            DictTerminalDto terminalDto = dictTerminalService.getSelfDeviceInfo(dictTerminalDto);
            if (terminalDto == null) {
                return XmlUtil.object2xmlResponse("1", "自助机配置信息不存在");
            }
            convertCardType4Self(dto, terminalDto);
        }
        // 真实查询
        JsonResult jsonResult = examApplyPatientService.getPatientApplyList(dto);
        if (jsonResult == null || jsonResult.getResult() == null) {
            return XmlUtil.object2xmlResponse("1", "您的检查申请单没有要检查的项目，如有疑问请到护士站咨询");
        }
        if (!"0".equals(jsonResult.getStatus())) {
            return XmlUtil.object2xmlResponse("1", jsonResult.getMessage());
        }
        List<ExamApplyDto> examApplyDtoList = (List<ExamApplyDto>) jsonResult.getResult();
        if (examApplyDtoList == null || examApplyDtoList.isEmpty()) {
            return XmlUtil.object2xmlResponse("1", "您的检查申请单没有要检查的项目，如有疑问请到护士站咨询");
        }
        // 组装移动端返回数据
        List<ResponseDto> responseDtoList = new ArrayList<ResponseDto>();
        for (ExamApplyDto examApplyDto : examApplyDtoList) {
            int scheduleStatus = StringUtils.isNotBlank(examApplyDto.getScheduleStatus()) ? Integer.parseInt(examApplyDto.getScheduleStatus()) : 0;
            if (scheduleStatus < 3) {
                ResponseDto responseDto = resultToNode(examApplyDto, dto.getTerminalType(), dto.getApplication());
                responseDtoList.add(responseDto);
            } else {
                if (!ConstantsUtil.TerminalType.MOBILE.getType().equals(dto.getTerminalType())) {
                    continue;
                }
                ResponseDto responseDto = resultToNode(examApplyDto, dto.getTerminalType(), dto.getApplication());
                responseDtoList.add(responseDto);
            }
        }
        if (responseDtoList.size() > 0) {
            return XmlUtil.object2xmlResponse("ExamApply", responseDtoList, jsonResult.getStatus(), jsonResult.getMessage());
        }
        return XmlUtil.object2xmlResponse("1", "您的检查申请单没有要检查的项目，如有疑问请到护士站咨询");
    }

    private String getSchedulePlans(String req, String terminalType) {
        ExamApplyDto dto = XmlUtil.xmlRequest2Bean(req, ExamApplyDto.class, "sickId,icCard,examClass,reqDateStart,reqDateEnd,deptCode,patientSource,wardCode,terminalType,deviceId,planMode,application,deviceId,userId,userName,commonCard");
        if (StringUtils.isBlank(dto.getPlanMode())) {
            dto.setPlanMode("1");
        }
        dto.setHistoryFlag("1");
        if (StringUtils.isNotBlank(terminalType)) {
            dto.setTerminalType(terminalType);
        }
        if (StringUtils.isNotBlank(dto.getTerminalType()) && ConstantsUtil.TerminalType.SELF.getType().equals(dto.getTerminalType()) && StringUtils.isNotBlank(dto.getDeviceId())) {
            DictTerminalDto dictTerminalDto = new DictTerminalDto();
            dictTerminalDto.setIpAddress(dto.getDeviceId());
            DictTerminalDto terminalDto = dictTerminalService.getSelfDeviceInfo(dictTerminalDto);
            if (terminalDto == null) {
                return XmlUtil.object2xmlResponse("1", "自助机配置信息不存在");
            }
            convertCardType4Self(dto, terminalDto);
        }
        JsonResult jsonResult = examApplyPatientService.getPatientApplyList(dto);
        if (jsonResult == null || jsonResult.getResult() == null) {
            return XmlUtil.object2xmlResponse("1", "您的检查申请单没有要检查的项目，如有疑问请到护士站咨询");
        }
        if (!"0".equals(jsonResult.getStatus())) {
            return XmlUtil.object2xmlResponse("1", jsonResult.getMessage());
        }
        List<ExamApplyDto> examApplyDtoList = (List<ExamApplyDto>) jsonResult.getResult();
        if (examApplyDtoList == null || examApplyDtoList.isEmpty()) {
            return XmlUtil.object2xmlResponse("1", "您的检查申请单没有要检查的项目，如有疑问请到护士站咨询");
        }
        List<ResponseDto> responseDtoList = new ArrayList<ResponseDto>();
        for (ExamApplyDto examApplyDto : examApplyDtoList) {
            int scheduleStatus = StringUtils.isNotBlank(examApplyDto.getScheduleStatus()) ? Integer.parseInt(examApplyDto.getScheduleStatus()) : 0;
            if (scheduleStatus < 3) {
                ResponseDto responseDto = resultToNode(examApplyDto, dto.getTerminalType(), dto.getApplication());
                responseDtoList.add(responseDto);
            }
        }
        if (responseDtoList.size() > 0) {
            return XmlUtil.object2xmlResponse("ExamApply", responseDtoList, jsonResult.getStatus(), jsonResult.getMessage());
        }
        return XmlUtil.object2xmlResponse("1", "您的检查申请单没有要检查的项目，如有疑问请到护士站咨询");
    }

    /**
     * 获取申请单列表
     *
     * @param req
     * @param terminalType
     * @return
     */
    private String getExamApplyInfo(String req, String terminalType) {
        ExamApplyDto dto = XmlUtil.xmlRequest2Bean(req, ExamApplyDto.class, "applyNo,mergeNo,terminalType,application,deviceId,userId,userName");
        if (StringUtils.isBlank(dto.getMergeNo())) {
            ExamApplyItemDto examApplyItemDto = new ExamApplyItemDto();
            examApplyItemDto.setMergeNos(CommonUtil.split2List(dto.getApplyNo()));
            String mergeNo = getMergeNos(examApplyItemDto);
            dto.setMergeNo(mergeNo);
        }
        if (StringUtils.isNotBlank(terminalType)) {
            dto.setTerminalType(terminalType);
        }
        JsonResult jsonResult = examApplyPatientService.getExamApplyInfo(dto);

        ExamApplyDto examApplyDto = (ExamApplyDto) jsonResult.getResult();
        examApplyDto.setMergeNo(dto.getMergeNo());
        ResponseDto responseDto = resultToNode(examApplyDto, dto.getTerminalType(), dto.getApplication());
        return XmlUtil.object2xmlResponse("ExamApply", responseDto, jsonResult.getStatus(), jsonResult.getMessage());
    }

    private String getEnabledScheduleApms(String req, String terminalType) {
        ExamApplyDto dto = XmlUtil.xmlRequest2Bean(req, ExamApplyDto.class, "applyNo,scheduleDate,hospitalCode,mergeNo,terminalType,deviceId,userId,userName");
        if (StringUtils.isBlank(dto.getMergeNo())) {
            ExamApplyItemDto examApplyItemDto = new ExamApplyItemDto();
            examApplyItemDto.setMergeNos(CommonUtil.split2List(dto.getApplyNo()));
            String mergeNo = getMergeNos(examApplyItemDto);
            dto.setMergeNo(mergeNo);
        }
        if (StringUtils.isNotBlank(terminalType)) {
            dto.setTerminalType(terminalType);
        }
        JsonResult jsonResult = examApplyService.getApmList(dto);
        if (jsonResult.getResult() != null && "0".equals(jsonResult.getStatus())) {
            for (ExamApplyDto examApplyDto : (List<ExamApplyDto>) jsonResult.getResult()) {
                examApplyDto.setScheduleApm(examApplyDto.getApm());
            }
        }
        return XmlUtil.object2xmlResponse("ScheduleApm", jsonResult.getResult(), jsonResult.getStatus(), jsonResult.getMessage());
    }

    private String isGestational(String req, String terminalType) {
        ExamApplyDto dto = XmlUtil.xmlRequest2Bean(req, ExamApplyDto.class, "sickId,icCard,terminalType,deviceId,userId,userName,commonCard");
        if (StringUtils.isNotBlank(terminalType)) {
            dto.setTerminalType(terminalType);
        }
        if (StringUtils.isNotBlank(dto.getTerminalType()) && ConstantsUtil.TerminalType.SELF.getType().equals(dto.getTerminalType()) && StringUtils.isNotBlank(dto.getDeviceId())) {
            DictTerminalDto dictTerminalDto = new DictTerminalDto();
            dictTerminalDto.setIpAddress(dto.getDeviceId());
            DictTerminalDto terminalDto = dictTerminalService.getSelfDeviceInfo(dictTerminalDto);
            if (terminalDto == null) {
                return XmlUtil.object2xmlResponse("1", "自助机配置信息不存在");
            }
            convertCardType4Self(dto, terminalDto);
        }
        JsonResult jsonResult = examApplyService.isGestation(dto);
        if (jsonResult == null) {
            return XmlUtil.object2xmlResponse("1", "没有您的检查申请记录，如有疑问请到护士站咨询");
        }
        if ("0".equals(jsonResult.getStatus())) {
            List<Object> resultList = (List<Object>) jsonResult.getResult();
            return XmlUtil.object2xmlResponse(resultList.get(0), jsonResult.getStatus(), jsonResult.getMessage());
        }
        return XmlUtil.object2xmlResponse(jsonResult.getStatus(), jsonResult.getMessage());
    }

    private String editLastMensesDate(String req, String terminalType) {
        String reqStr = req.replace("weeks", "gestationalWeeks").replace("days", "gestationalDays");
        ExamApplyDto dto = XmlUtil.xmlRequest2Bean(reqStr, ExamApplyDto.class, "sickId,icCard,lastMensesDate,gestationalWeeks,gestationalDays,terminalType,deviceId,userId,userName,commonCard");
        if (StringUtils.isNotBlank(terminalType)) {
            dto.setTerminalType(terminalType);
        }
        if (StringUtils.isNotBlank(dto.getTerminalType()) && ConstantsUtil.TerminalType.SELF.getType().equals(dto.getTerminalType()) && StringUtils.isNotBlank(dto.getDeviceId())) {
            DictTerminalDto dictTerminalDto = new DictTerminalDto();
            dictTerminalDto.setIpAddress(dto.getDeviceId());
            DictTerminalDto terminalDto = dictTerminalService.getSelfDeviceInfo(dictTerminalDto);
            if (terminalDto == null) {
                return XmlUtil.object2xmlResponse("1", "自助机配置信息不存在");
            }
            convertCardType4Self(dto, terminalDto);
        }
        if (StringUtils.isNotBlank(dto.getLastMensesDate())) {
            dto.setLastMensesDate(DateUtil.format(dto.getLastMensesDate(), "yyyy-MM-dd"));
        }
        JsonResult jsonResult = examApplyService.editLastMensesDate(dto);
        return XmlUtil.object2xmlResponse(jsonResult.getStatus(), jsonResult.getMessage());
    }

    private String setScheduleApm(String req, String terminalType) {
        ExamApplyDto dto = XmlUtil.xmlRequest2Bean(req, ExamApplyDto.class, "applyNo,scheduleDate,scheduleApm,hospitalCode,mergeNo,terminalType,deviceId,userId,userName,startTime,endTime,earliestDate,earliestTime");
        if (StringUtils.isBlank(dto.getMergeNo())) {
            ExamApplyItemDto examApplyItemDto = new ExamApplyItemDto();
            examApplyItemDto.setMergeNos(CommonUtil.split2List(dto.getApplyNo()));
            String mergeNo = getMergeNos(examApplyItemDto);
            dto.setMergeNo(mergeNo);
        }
        if (StringUtils.isNotBlank(terminalType)) {
            dto.setTerminalType(terminalType);
        }
        JsonResult jsonResult = examApplyService.setApm(dto);
        String status = jsonResult.getStatus();
        if ("2".equals(status) || "您已经预约该时段,请选择别的时段！".equals(jsonResult.getMessage())) {
            status = "0";
        }
        return XmlUtil.object2xmlResponse(status, jsonResult.getMessage());
    }

    private String delScheduleApm(String req, String terminalType) {
        ExamApplyDto dto = XmlUtil.xmlRequest2Bean(req, ExamApplyDto.class, "applyNo,mergeNo,terminalType,deviceId,userId,userName");
        if (StringUtils.isBlank(dto.getMergeNo())) {
            ExamApplyItemDto examApplyItemDto = new ExamApplyItemDto();
            examApplyItemDto.setMergeNos(CommonUtil.split2List(dto.getApplyNo()));
            String mergeNo = getMergeNos(examApplyItemDto);
            dto.setMergeNo(mergeNo);
        }
        if (StringUtils.isNotBlank(terminalType)) {
            dto.setTerminalType(terminalType);
        }
        JsonResult jsonResult = examApplyService.delApm(dto);
        if (jsonResult.getStatus().equals("0")) {
            ExamApplyDto ea = (ExamApplyDto) jsonResult.getResult();

            if (!"01".equals(ea.getScheduleStatus())) {

                ExamApplyDto examApplyDto = new ExamApplyDto();
                List<String> applyNos = CommonUtil.mergeNo2ApplyNos(dto.getMergeNo());
                examApplyDto.setApplyNos(applyNos);
                String actType = ConstantsUtil.ActType.CANCEL.getType();
                examApplyDto.setActType(actType);
                ExamApplyDto applyResult = (ExamApplyDto) jsonResult.getResult();
                examApplyDto.setExamClass(applyResult.getExamClass());
                examApplyDto.setScheduledId(applyResult.getScheduledId());
                examApplyDto.setMergeNo(applyResult.getMergeNo());
                examApplyDto.setApplyNo(dto.getApplyNo());
                examApplyDto.setTerminalType(applyResult.getTerminalType());
                examApplyDto.setScheduleDate(applyResult.getScheduleDate());
                examApplyDto.setName(applyResult.getName());
                examApplyDto.setSex(applyResult.getSex());
                examApplyDto.setAge(applyResult.getAge());
                examApplyDto.setScheduleApm(applyResult.getScheduleApm());
                examApplyDto.setScheduleHospitalName(applyResult.getScheduleHospitalName());
                examApplyDto.setPerformDeptName(applyResult.getPerformDeptName());
                String itemNameStr = "";
                for (ExamApplyItemDto item : applyResult.getItems()) {
                    itemNameStr = itemNameStr + item.getItemName() + ";";
                }
                examApplyDto.setItemNameStr(itemNameStr);
                configService.isScheduleSync(examApplyDto, actType, ConstantsUtil.ScheduleStatus.NINE.getStatus());
            }
        }
        return XmlUtil.object2xmlResponse(jsonResult.getStatus(), jsonResult.getMessage());
    }

    private String editScheduleStatus(String req, String terminalType) {
        ExamApplyDto dto = XmlUtil.xmlRequest2Bean(req, ExamApplyDto.class, "applyNo,scheduleStatus,itemNo,terminalType,examNo,patLocalId,queueNo,deviceId,userId,userName,reqUser,reqSystem,earliestDate,earliestTime");
        if (StringUtils.isNotBlank(terminalType)) {
            dto.setTerminalType(terminalType);
        }
        JsonResult jsonResult = examScheduleService.editScheduleStatus(dto);
        return XmlUtil.object2xmlResponse(jsonResult.getStatus(), jsonResult.getMessage());
    }

    private String confirmScheduleApm(String req, String terminalType) {
        ExamApplyDto dto = XmlUtil.xmlRequest2Bean(req, ExamApplyDto.class, "applyNo,mergeNo,terminalType,application,deviceId,userId,userName");
        if (StringUtils.isNotBlank(terminalType)) {
            dto.setTerminalType(terminalType);
        }
        JsonResult jsonResult = null;
        try {
            jsonResult = examApplyService.confirmApm(dto);
        } catch (Exception e) {
            return XmlUtil.object2xmlResponse("1", e.getMessage());
        }
        if (!"0".equals(jsonResult.getStatus())) {
            return XmlUtil.object2xmlResponse(jsonResult.getStatus(), jsonResult.getMessage());
        }
        ExamApplyDto examApplyDto = new ExamApplyDto();
        List<String> applyNos = CommonUtil.mergeNo2ApplyNos(dto.getMergeNo());
        examApplyDto.setApplyNos(applyNos);
        ExamApplyDto applyResult = (ExamApplyDto) jsonResult.getResult();
        String actType = applyResult.getActType();
        examApplyDto.setActType(actType);
        examApplyDto.setExamClass(applyResult.getExamClass());
        examApplyDto.setScheduledId(applyResult.getScheduledId());
        examApplyDto.setMergeNo(applyResult.getMergeNo());
        examApplyDto.setApplyNo(dto.getApplyNo());
        examApplyDto.setTerminalType(applyResult.getTerminalType());
        boolean isSucc = configService.isScheduleSync(examApplyDto, actType, ConstantsUtil.ScheduleStatus.EIGHT.getStatus());
        if (!isSucc) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return XmlUtil.object2xmlResponse("1", "预约失败,请联系工作人员");
        }
        ResponseDto responseDto = resultToNode((ExamApplyDto) jsonResult.getResult(), dto.getTerminalType(), dto.getApplication(), dto.getDeviceId());
        return XmlUtil.object2xmlResponse("ExamApply", responseDto, jsonResult.getStatus(), jsonResult.getMessage());
    }

    private String editScheduleApm(String req, String terminalType) {
        ExamApplyDto dto = XmlUtil.xmlRequest2Bean(req, ExamApplyDto.class, "applyNo,scheduleDate,scheduleApm,hospitalCode,mergeNo,terminalType,application,deviceId,userId,userName,startTime,endTime");
        if (StringUtils.isBlank(dto.getMergeNo())) {
            ExamApplyItemDto examApplyItemDto = new ExamApplyItemDto();
            examApplyItemDto.setMergeNos(CommonUtil.split2List(dto.getApplyNo()));
            String mergeNo = getMergeNos(examApplyItemDto);
            dto.setMergeNo(mergeNo);
        }
        if (StringUtils.isNotBlank(terminalType)) {
            dto.setTerminalType(terminalType);
        }
        JsonResult jsonResult = examApplyService.editApm(dto);
        if ("0".equals(jsonResult.getStatus())) {
            if (jsonResult.getStatus().equals("0")) {
                ExamApplyDto examApplyDto = new ExamApplyDto();
                List<String> applyNos = CommonUtil.mergeNo2ApplyNos(dto.getMergeNo());
                examApplyDto.setApplyNos(applyNos);
                String actType = ConstantsUtil.ActType.UPDATE.getType();
                examApplyDto.setActType(actType);
                ExamApplyDto applyResult = (ExamApplyDto) jsonResult.getResult();
                examApplyDto.setExamClass(applyResult.getExamClass());
                examApplyDto.setScheduledId(applyResult.getScheduledId());
                examApplyDto.setMergeNo(applyResult.getMergeNo());
                examApplyDto.setApplyNo(dto.getApplyNo());
                examApplyDto.setTerminalType(applyResult.getTerminalType());
                boolean isSucc = configService.isScheduleSync(examApplyDto, actType, ConstantsUtil.ScheduleStatus.EIGHT.getStatus());
                if (!isSucc) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return XmlUtil.object2xmlResponse("1", "预约失败,请联系工作人员");
                }
            }
            ResponseDto responseDto = resultToNode((ExamApplyDto) jsonResult.getResult(), dto.getTerminalType(), dto.getApplication(), dto.getDeviceId());
            return XmlUtil.object2xmlResponse("ExamApply", responseDto, jsonResult.getStatus(), jsonResult.getMessage());
        }
        return XmlUtil.object2xmlResponse(jsonResult.getStatus(), jsonResult.getMessage());
    }

    private String getLastMensesDate(String req, String terminalType) {
        JSONObject xmlJsonObject = XmlUtil.xml2JSON(req);
        String sickId = xmlJsonObject.getString("sickId");
        if (StringUtils.isBlank(sickId)) {
            return XmlUtil.object2xmlResponse("1", "sickId不能为空");
        }
        String lastMensesDateStr = commonService.getLastMensesDate(sickId);
        Map<String, Object> resultMap = new HashMap<>();
        if (StringUtils.isNotBlank(lastMensesDateStr)) {
            LocalDate lastMensesDate = LocalDate.parse(lastMensesDateStr, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            int allDays = (int) (LocalDate.now().toEpochDay() - lastMensesDate.toEpochDay());
            int weeks = allDays / 7;
            int days = allDays % 7;
            resultMap.put("weeks", weeks);
            resultMap.put("days", days);
            resultMap.put("lastMensesDate", lastMensesDateStr);
        }
        return XmlUtil.object2xmlResponse(resultMap, "0", "成功");
    }

    /**
     * 报到-已预约申请单
     *
     * @param req
     * @param terminalType
     * @return
     */
    private String signInScheduled(String req, String terminalType) {
        ExamApplyDto dto = XmlUtil.xmlRequest2Bean(req, ExamApplyDto.class, "applyNo,terminalType,deviceId,userId,userName,mergeNo,application,commonCard");
        if (StringUtils.isNotBlank(terminalType)) {
            dto.setTerminalType(terminalType);
        }
        // 自助机条件过滤
        if (ConstantsUtil.TerminalType.SELF.getType().equals(dto.getTerminalType())) {
            DictTerminalDto dictTerminalDto = new DictTerminalDto();
            dictTerminalDto.setIpAddress(dto.getDeviceId());
            DictTerminalDto terminalDto = dictTerminalService.getSelfDeviceInfo(dictTerminalDto);
            if (terminalDto == null || StringUtils.isBlank(terminalDto.getSignExamClass())) {
                return XmlUtil.object2xmlResponse("1", "本机不能办理检查报到，如有疑问请到护士站咨询！");
            }
            convertCardType4Self(dto, terminalDto);
            dto.setScheduledDate(DateUtil.getCurrDateStr());
            dto.setUnSignQueues(CommonUtil.split2List(terminalDto.getUnSignQueue()));
            dto.setExamClasses(CommonUtil.split2List(terminalDto.getSignExamClass()));
            dto.setScheduleHospital(terminalDto.getHospitalCode());
            List<ExamApplyDto> list = examApplyMapper.findByApplyNo(dto);
            if (CollectionUtils.isEmpty(list)) {
                String strReturn = "本机仅提供" + terminalDto.getSignExamClass() + "检查报到，如有疑问请到护士站咨询！";
                String reminder = configService.customReminder(ConstantsUtil.ConfigName.NO_SIGN_QUEUE_BY_SELF_REMINDER.getKey(), "", terminalDto.getSignExamClass());
                if (StringUtils.isNotBlank(reminder)) {
                    strReturn = reminder;
                }
                return XmlUtil.object2xmlResponse("1", strReturn);
            }
        }

        if (StringUtils.isBlank(dto.getMergeNo())) {
            List<ExamApplyDto> list2 = examApplyMapper.findByApplyNo(dto);
            StringBuffer applyNos = new StringBuffer();
            for (ExamApplyDto examApplyDto : list2) {
                if (list2.get(0).getScheduledId().equals(examApplyDto.getScheduledId())) {
                    applyNos.append(examApplyDto.getApplyNo() + "-" + examApplyDto.getItemNo());
                }
            }
            dto.setMergeNo(applyNos.substring(1));
        }
        // 签到操作
        JsonResult jsonResult = examApplyService.signinSchedule(dto);
        if ("0".equals(jsonResult.getStatus())) {
            ExamApplyDto ea = (ExamApplyDto) jsonResult.getResult();
            if (StringUtils.isBlank(ea.getIsSign())) {
                ExamApplyDto examApplyDto2 = new ExamApplyDto();
                List<String> applyNos2 = CommonUtil.mergeNo2ApplyNos(dto.getMergeNo());
                examApplyDto2.setApplyNos(applyNos2);
                String actType = ConstantsUtil.ActType.SIGN.getType();
                examApplyDto2.setActType(actType);
                ExamApplyDto applyResult = (ExamApplyDto) jsonResult.getResult();
                examApplyDto2.setExamClass(applyResult.getExamClass());
                examApplyDto2.setScheduledId(applyResult.getScheduledId());
                examApplyDto2.setMergeNo(applyResult.getMergeNo());
                examApplyDto2.setApplyNo(dto.getApplyNo());
                examApplyDto2.setTerminalType(applyResult.getTerminalType());
                configService.isScheduleSync(examApplyDto2, actType, ConstantsUtil.ScheduleStatus.TEN.getStatus());
            }
        }
        return XmlUtil.object2xmlResponse("ExamApply", resultToNode((ExamApplyDto) jsonResult.getResult(), dto.getTerminalType(), dto.getApplication()), jsonResult.getStatus(), jsonResult.getMessage());
    }

    private String signInScheduledMulti(String req, String terminalType) {
        ExamApplyDto dto = XmlUtil.xmlRequest2Bean(req, ExamApplyDto.class, "sickId,icCard,examClass,terminalType,deviceId,application,userId,userName,commonCard");
        if (StringUtils.isNotBlank(terminalType)) {
            dto.setTerminalType(terminalType);
        }
        DictTerminalDto dictTerminalDto = new DictTerminalDto();
        dictTerminalDto.setIpAddress(dto.getDeviceId());
        DictTerminalDto terminalDto = dictTerminalService.getSelfDeviceInfo(dictTerminalDto);
        if (terminalDto == null || StringUtils.isBlank(terminalDto.getSignExamClass())) {
            return XmlUtil.object2xmlResponse("1", "本机不能办理检查报到，如有疑问请到护士站咨询！");
        }
        convertCardType4Self(dto, terminalDto);
        List<String> sickIds = null;
        if (StringUtils.isNotBlank(dto.getSickId())) {
            sickIds = (List<String>) CommonUtil.split2List(dto.getSickId());
        } else {
            sickIds = commonService.getSickIds(dto);
        }
        if (sickIds.size() == 0) {
            String strReturn = "您的检查申请单信息不存在，如有疑问请到护士站咨询！";
            String reminder = configService.customReminder(ConstantsUtil.ConfigName.NO_QUERY_EXAM_APPLY_REMINDER.getKey(), "", "");
            if (StringUtils.isNotBlank(reminder)) {
                strReturn = reminder;
            }
            return XmlUtil.object2xmlResponse("1", strReturn);
        }
        dto.setScheduledDate(DateUtil.getCurrDateStr());
        dto.setSickIds(sickIds);
        dto.setUnSignQueues(CommonUtil.split2List(terminalDto.getUnSignQueue()));
        dto.setExamClasses(CommonUtil.split2List(terminalDto.getSignExamClass()));
        dto.setScheduleHospital(terminalDto.getHospitalCode());
        List<ExamApplyDto> list = examApplyMapper.findByApplyNo(dto);
        List<Object> resultList = new ArrayList<>();
        List<String> tmpList = new ArrayList<>();
        if (list == null || list.size() == 0) {
            String strReturn2 = "本机仅提供" + terminalDto.getSignExamClass() + "检查报到，如有疑问请到护士站咨询！";
            String reminder2 = configService.customReminder(ConstantsUtil.ConfigName.NO_SIGN_QUEUE_BY_SELF_REMINDER.getKey(), "", terminalDto.getSignExamClass());
            if (StringUtils.isNotBlank(reminder2)) {
                strReturn2 = reminder2;
            }
            return XmlUtil.object2xmlResponse("1", strReturn2);
        }
        list.removeIf(eaDto -> "01".equals(eaDto.getScheduleStatus()));
        if (ConstantsUtil.TerminalType.SELF.getType().equals(dto.getTerminalType()) && StringUtils.isNotBlank(dto.getApplication())) {
            list.removeIf(eaDto -> StringUtils.isNotBlank(eaDto.getExamStatus()) && Integer.parseInt(eaDto.getExamStatus()) >= 10);
        }
        for (ExamApplyDto examApply : list) {
            if (!tmpList.contains(examApply.getScheduledId())) {
                Map<String, Object> map = new HashMap<String, Object>();
                List<String> repeatSignByExamClassList = configService.getConfigListByName(ConstantsUtil.ConfigName.REPEAT_SIGN_BY_EXAM_CLASS.getKey());
                if (ConstantsUtil.TerminalType.SELF.getType().equals(dto.getTerminalType()) && StringUtils.isNotBlank(examApply.getExamStatus()) && Integer.parseInt(examApply.getExamStatus()) >= 10 && (repeatSignByExamClassList.isEmpty() || !repeatSignByExamClassList.contains(examApply.getExamClass()))) {
                    ResponseDto responseDto = resultToNode(examApply, dto.getTerminalType(), dto.getApplication());
                    map.put("msgCode", "1");
                    if (StringUtils.isBlank(dto.getApplication())) {
                        map.put("msgDesc", "您今天的" + examApply.getExamClass() + "检查预约已经报到了，<br/>【已报到信息】<br/>排队号：" + responseDto.getQueueNo() + "<br/>预约时间：" + responseDto.getScheduleDate() + "<br/>预约时段：" + responseDto.getScheduleApm() + "<br/>检查队列：" + responseDto.getQueueName() + "<br/>检查地点：" + responseDto.getLocation());
                    } else {
                        map.put("msgDesc", "您今天的" + examApply.getExamClass() + "检查预约已经报到了，【已报到信息】排队号：" + responseDto.getQueueNo() + "，预约时间：" + responseDto.getScheduleDate() + "，预约时段：" + responseDto.getScheduleApm() + "，检查队列：" + responseDto.getQueueName() + "，检查地点：" + responseDto.getLocation());
                    }
                } else {
                    ExamApplyDto applyDto = new ExamApplyDto();
                    applyDto.setApplyNo(examApply.getApplyNo());
                    applyDto.setTerminalType(dto.getTerminalType());
                    applyDto.setUserId(dto.getUserId());
                    applyDto.setUserName(dto.getUserName());
                    if (StringUtils.isNotBlank(examApply.getScheduleDate()) && DateUtil.isToday(examApply.getScheduleDate())) {
                        StringBuffer applyNos = new StringBuffer();
                        for (ExamApplyDto apply : list) {
                            if (examApply.getScheduledId().equals(apply.getScheduledId())) {
                                applyNos.append("," + apply.getApplyNo() + "-" + apply.getItemNo());
                            }
                        }
                        applyDto.setMergeNo(applyNos.substring(1));
                        JsonResult jsonResult = examApplyService.signinSchedule(applyDto);
                        map.put("msgCode", jsonResult.getStatus());
                        map.put("msgDesc", jsonResult.getMessage());
                        if ("0".equals(jsonResult.getStatus())) {
                            ExamApplyDto applyResult = (ExamApplyDto) jsonResult.getResult();
                            map.put("ExamApply", resultToNode(applyResult, dto.getTerminalType(), dto.getApplication()));
                            if (StringUtils.isBlank(applyResult.getIsSign())) {
                                ExamApplyDto examApplyDto = new ExamApplyDto();
                                List<String> applyNoList = CommonUtil.mergeNo2ApplyNos(applyDto.getMergeNo());
                                examApplyDto.setApplyNos(applyNoList);
                                String actType = ConstantsUtil.ActType.SIGN.getType();
                                examApplyDto.setActType(actType);
                                examApplyDto.setExamClass(applyResult.getExamClass());
                                examApplyDto.setScheduledId(applyResult.getScheduledId());
                                examApplyDto.setMergeNo(applyResult.getMergeNo());
                                examApplyDto.setApplyNo(applyDto.getApplyNo());
                                configService.isScheduleSync(examApplyDto, actType, ConstantsUtil.ScheduleStatus.TEN.getStatus());
                            }
                        }
                    }
                }
                resultList.add(map);
                tmpList.add(examApply.getScheduledId());
            }
        }
        return XmlUtil.object2xmlResponse("Message", resultList, "0", "成功");
    }

    private String getScheduleSigned(String req, String terminalType) {
        ExamApplyDto dto = XmlUtil.xmlRequest2Bean(req, ExamApplyDto.class, "sickId,icCard,deviceId,userId,userName,examClass,terminalType,application,commonCard");
        if (StringUtils.isBlank(dto.getSickId()) && StringUtils.isBlank(dto.getIcCard())) {
            return XmlUtil.object2xmlResponse("1", "sickId和icCard不能同时为空");
        }
        if (StringUtils.isNotBlank(terminalType)) {
            dto.setTerminalType(terminalType);
        }
        if (StringUtils.isNotBlank(dto.getSickId())) {
            QueryWrapper<ExamApply> qw = new QueryWrapper<>();
            qw.eq("SICK_ID", dto.getSickId());
            ExamApply examApply = examApplyService.getOne(qw, false);
            dto.setCommonCard(examApply.getIcCard());
            dto.setSickId(null);
        } else if (StringUtils.isNotBlank(dto.getTerminalType()) && ConstantsUtil.TerminalType.SELF.getType().equals(dto.getTerminalType()) && StringUtils.isNotBlank(dto.getDeviceId())) {
            DictTerminalDto dictTerminalDto = new DictTerminalDto();
            dictTerminalDto.setIpAddress(dto.getDeviceId());
            DictTerminalDto terminalDto = dictTerminalService.getSelfDeviceInfo(dictTerminalDto);
            if (terminalDto == null) {
                return XmlUtil.object2xmlResponse("1", "自助机配置信息不存在");
            }
            convertCardType4Self(dto, terminalDto);
        }
        dto.setDateType("0");
        dto.setScheduleStatus("3");
        dto.setStartDate(LocalDate.now().toString());
        dto.setEndDate(LocalDate.now().toString());
        JsonResult jsonResult = examApplyService.getExamSchedules(dto);
        if (jsonResult == null || jsonResult.getResult() == null) {
            return XmlUtil.object2xmlResponse("1", "您的检查申请单没有已报到的项目，如有疑问请到护士站咨询");
        }
        if (!"0".equals(jsonResult.getStatus())) {
            return XmlUtil.object2xmlResponse("1", jsonResult.getMessage());
        }
        List<ExamApplyDto> examApplyDtoList = (List<ExamApplyDto>) jsonResult.getResult();
        if (examApplyDtoList == null || examApplyDtoList.isEmpty()) {
            return XmlUtil.object2xmlResponse("1", "您的检查申请单没有已报到的项目，如有疑问请到护士站咨询");
        }
        List<ResponseDto> responseDtoList = new ArrayList<ResponseDto>();
        for (ExamApplyDto examApplyDto : examApplyDtoList) {
            ResponseDto responseDto = resultToNode(examApplyDto, dto.getTerminalType(), dto.getApplication());
            responseDtoList.add(responseDto);
        }
        if (responseDtoList.size() > 0) {
            return XmlUtil.object2xmlResponse("ExamApply", responseDtoList, jsonResult.getStatus(), jsonResult.getMessage());
        }
        return XmlUtil.object2xmlResponse("1", "您的检查申请单没有已报到的项目，如有疑问请到护士站咨询");
    }

    private String getEnabledSignClasses(String req, String terminalType) {
        String reqStr = req.replace("deviceId", "ipAddress");
        DictExamClassDto dto = (DictExamClassDto) XmlUtil.xmlRequest2Bean(reqStr, DictExamClassDto.class, "ipAddress,terminalType,sickId,icCard,application,commonCard");
        if (StringUtils.isNotBlank(terminalType)) {
            dto.setTerminalType(terminalType);
        }
        JsonResult jsonResult = scheduleSignService.getEnabledSignClasses(dto, terminalType);
        List<Map> list = new ArrayList<Map>();
        if (jsonResult.getResult() != null) {
            List<Object> jsonResultList = (List<Object>) jsonResult.getResult();
            for (Object result : jsonResultList) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("name", result);
                list.add(map);
            }
        }
        return XmlUtil.object2xmlResponse("ExamClass", list, jsonResult.getStatus(), jsonResult.getMessage());
    }

    private String editPrintFlag(String req, String terminalType) {
        ExamApplyDto dto = XmlUtil.xmlRequest2Bean(req, ExamApplyDto.class, "applyNo,mergeNo,terminalType,deviceId,userId,userName");
        if (StringUtils.isNotBlank(terminalType)) {
            dto.setTerminalType(terminalType);
        }
        boolean editResult = examScheduleService.printCntPlusOne(dto);
        return editResult ? XmlUtil.object2xmlResponse("0", "成功") : null;
    }

    private String getScheduleExpireDate(String req, String terminalType) {
        ExamApplyDto dto = XmlUtil.xmlRequest2Bean(req, ExamApplyDto.class, "terminalType,applyNo,hospitalCode,deviceId,userId,userName,mergeNo");
        JsonResult jsonResult = examApplyService.getScheduleExpireDate(dto);
        return XmlUtil.object2xmlResponse(jsonResult.getResult(), jsonResult.getStatus(), jsonResult.getMessage());
    }

    private String getExamNotices(String req, String terminalType) {
        ExamApplyDto dto = XmlUtil.xmlRequest2Bean(req, ExamApplyDto.class, "applyNo,mergeNo,terminalType,deviceId,userId,userName");
        Map<String, Object> resultMap = new HashMap<String, Object>();
        List<String> reminders = new ArrayList<String>();
        List<String> notices = new ArrayList<String>();
        ExamApplyItemDto examApplyItemDto = new ExamApplyItemDto();
        if (StringUtils.isBlank(dto.getMergeNo())) {
            examApplyItemDto.setMergeNos(CommonUtil.split2List(dto.getApplyNo()));
            String mergeNo = getMergeNos(examApplyItemDto);
            dto.setMergeNo(mergeNo);
        } else {
            examApplyItemDto.setMergeNos(CommonUtil.mergeNo2ApplyNos(dto.getMergeNo()));
        }
        if (StringUtils.isNotBlank(terminalType)) {
            dto.setTerminalType(terminalType);
        }
        List<ExamApplyDto> list = examApplyMapper.getByIcCard(dto);
        if (list != null && !list.isEmpty()) {
            ExamApplyDto es = list.get(0);
            if (es != null) {
                String scheduleStatus = "2";
                if (StringUtils.isNotBlank(es.getExamStatus())) {
                    scheduleStatus = CommonUtil.turnScheduleStatus(es.getExamStatus());
                } else {
                    scheduleStatus = CommonUtil.turnScheduleStatus(es.getScheduleStatus());
                }
                String reminderType = "2".equals(scheduleStatus) ? ConstantsUtil.ReminderType.SCHEDULE.getType() : ConstantsUtil.ReminderType.SIGN.getType();
                es.setMergeNo(dto.getMergeNo());
                es.setTerminalType(dto.getTerminalType());
                es.setType(reminderType);
                JsonResult jsonResult = examApplyPatientService.getExamApplyInfo(es);
                ExamApplyDto examApplyDto = (ExamApplyDto) jsonResult.getResult();
                if (examApplyDto != null) {
                    if (!examApplyDto.getReminders().isEmpty()) {
                        reminders = examApplyDto.getReminders();
                    }
                    if (!examApplyDto.getNotices().isEmpty()) {
                        notices = examApplyDto.getNotices();
                    }
                }
            }
        }
        Map<String, Object> tempMap = new HashMap<String, Object>();
        tempMap.put("Reminder", reminders);
        resultMap.put("Reminders", tempMap);
        Map<String, Object> tempMap2 = new HashMap<String, Object>();
        tempMap2.put("Notice", notices);
        resultMap.put("Notices", tempMap2);
        return XmlUtil.object2xmlResponse(resultMap, "0", "成功");
    }

    @Override
    public String GetWorkDateList(String req) {
        DictWorkDateDto dto = XmlUtil.xmlRequest2Bean(req, DictWorkDateDto.class, "startDate,endDate,flag");
        List<DictWorkDate> workDates = dictWorkDateService.getList(dto);
        return XmlUtil.object2xmlResponse("WorkDate", workDates, "0", "成功");
    }

    @Override
    public String GetPatientInfoForMobile(String req) {
        return getPatientInfo(req, ConstantsUtil.TerminalType.MOBILE.getType());
    }

    @Override
    public String GetWaitingInfo(String req) {
        ExamApplyDto dto = XmlUtil.xmlRequest2Bean(req, ExamApplyDto.class, "examNo,examClass");
        WaitingDto waitingDto = commonService.getWaitingInfo(dto);
        return XmlUtil.object2xmlResponse("Waiting", waitingDto, "0", "成功");
    }

    @Override
    public String GetResponseByMethodName(String methodName, String req) {
        Method method = null;
        String result = null;
        try {
            log.info("【GetResponseByMethodName】(请求接口:{},请求内容:{})", methodName, req);
            method = getClass().getDeclaredMethod(methodName, String.class);
            result = (String) method.invoke(this, req);
        } catch (Exception e) {
            e.printStackTrace();
            return "<Response><code>-1</code><desc>未定义此接口</desc></Response>";
        }
        return result;
    }

    @Override
    public String AutoSchedule(String req) {
        ExamApplyDto dto = XmlUtil.xmlRequest2Bean(req, ExamApplyDto.class, "sickId,applyNo,terminalType,application");
        if (StringUtils.isBlank(dto.getTerminalType())) {
            dto.setTerminalType(ConstantsUtil.TerminalType.SYSTEM.getType());
        }
        dto.setPlanMode("1");
        JsonResult jsonResult = examApplyPatientService.getPatientApplyList(dto);
        if (jsonResult == null || jsonResult.getResult() == null) {
            return XmlUtil.object2xmlResponse("1", "未查询到申请单信息，如有疑问请到护士站咨询");
        }
        if (!"0".equals(jsonResult.getStatus())) {
            return XmlUtil.object2xmlResponse("1", jsonResult.getMessage());
        }
        List<ExamApplyDto> examApplyDtoList = (List<ExamApplyDto>) jsonResult.getResult();
        if (examApplyDtoList == null && examApplyDtoList.isEmpty()) {
            return XmlUtil.object2xmlResponse("1", "未查询到申请单信息，如有疑问请到护士站咨询");
        }
        List<ResponseDto> responseDtos = new ArrayList<ResponseDto>();
        List<String> failMsg = new ArrayList<String>();
        boolean b = false;
        for (ExamApplyDto applyDto : examApplyDtoList) {
            if ("0".equals(applyDto.getScheduleStatus())) {
                ResponseDto responseDto = resultToNode(applyDto, dto.getTerminalType(), dto.getApplication());
                responseDto.setDesc("暂不支持预约！");
                responseDtos.add(responseDto);
                failMsg.add(applyDto.getApplyNo());
            } else {
                dto.setApplyNo(applyDto.getApplyNo());
                dto.setMergeNo(applyDto.getMergeNo());
                dto.setHospitalCode(applyDto.getHospitalCode());
                JsonResult confirmResult = null;
                try {
                    confirmResult = examApplyService.confirmApm(dto);
                } catch (Exception e) {
                    confirmResult = new JsonResult();
                    confirmResult.setStatus("1");
                    confirmResult.setMessage(e.getMessage());
                }
                if ("0".equals(confirmResult.getStatus())) {
                    b = true;
                    ExamApplyDto examApplyDto = new ExamApplyDto();
                    List<String> applyNos = CommonUtil.mergeNo2ApplyNos(dto.getMergeNo());
                    examApplyDto.setApplyNos(applyNos);
                    ExamApplyDto applyResult = (ExamApplyDto) confirmResult.getResult();
                    String actType = applyResult.getActType();
                    examApplyDto.setActType(actType);
                    examApplyDto.setExamClass(applyResult.getExamClass());
                    examApplyDto.setScheduledId(applyResult.getScheduledId());
                    examApplyDto.setMergeNo(applyResult.getMergeNo());
                    examApplyDto.setApplyNo(dto.getApplyNo());
                    boolean isSucc = configService.isScheduleSync(examApplyDto, actType, ConstantsUtil.ScheduleStatus.EIGHT.getStatus());
                    if (!isSucc) {
                        log.info(applyDto.getApplyNo() + " 预约信息推送失败");
                    }
                    ResponseDto responseDto2 = resultToNode((ExamApplyDto) confirmResult.getResult(), dto.getTerminalType(), dto.getApplication());
                    responseDtos.add(responseDto2);
                } else {
                    ResponseDto responseDto3 = resultToNode(applyDto, dto.getTerminalType(), dto.getApplication());
                    responseDto3.setDesc(confirmResult.getMessage());
                    responseDtos.add(responseDto3);
                    failMsg.add(applyDto.getApplyNo());
                }
            }
        }
        if (failMsg.size() == 0) {
            failMsg.add("成功");
        }
        if (b) {
            return XmlUtil.object2xmlResponse("ExamApply", responseDtos, "0", StringUtils.join((Iterable) failMsg, ","));
        }
        return XmlUtil.object2xmlResponse("ExamApply", responseDtos, "-1", StringUtils.join((Iterable) failMsg, ","));
    }

    @Override
    public String configToCache(String req) {
        ExamApplyDto dto = XmlUtil.xmlRequest2Bean(req, ExamApplyDto.class, "type");
        if (StringUtils.isBlank(dto.getType())) {
            return XmlUtil.object2xmlResponse("Response", "-1", "系统配置信息内容不能为空");
        }
        if (Objects.equals(dto.getType(), "REGULER")) {
            EhCacheUtil.removeForCacheName(ConstantsUtil.EhCacheName.regularCache.name());
            EhCacheUtil.removeForCacheName(ConstantsUtil.EhCacheName.ruleResultCache.name());
        } else if (Objects.equals(dto.getType(), "ALL")) {
            EhCacheUtil.removeForCacheName(ConstantsUtil.EhCacheName.regularCache.name());
            EhCacheUtil.removeForCacheName(ConstantsUtil.EhCacheName.ruleResultCache.name());
            EhCacheUtil.removeForCacheName(ConstantsUtil.EhCacheName.tokenCache.name());
            EhCacheUtil.removeForCacheName(ConstantsUtil.EhCacheName.scheduleTask.name());
            EhCacheUtil.removeForCacheName("configCache");
        } else {
            EhCacheUtil.removeForCacheName("configCache");
        }
        return XmlUtil.object2xmlResponse("Response", "0", "成功");
    }

    @Override
    public String cancelExamApply(String req) {
        ExamApplyDto dto = XmlUtil.xmlRequest2Bean(req, ExamApplyDto.class, "applyNo,reqUser,reqSystem,authcode");
        dto.setScheduleStatus("C");
        JsonResult jsonResult = examScheduleService.editScheduleStatus(dto);
        if (!Objects.equals("0", jsonResult.getStatus())) {
            return XmlUtil.object2xmlResponse("Response", "0", jsonResult.getMessage());
        }
        return XmlUtil.object2xmlResponse("Response", "0", "成功");
    }

    private String getMergeNos(ExamApplyItemDto examApplyItemDto) {
        List<ExamApplyItemDto> itemDtoList = examApplyItemMapper.getList(examApplyItemDto);
        if (itemDtoList != null && !itemDtoList.isEmpty()) {
            commonService.handItemCode(itemDtoList, "");
            StringBuffer mergeNos = new StringBuffer();
            for (ExamApplyItemDto itemDto : itemDtoList) {
                if (itemDto.getItNo() != null) {
                    mergeNos.append("," + itemDto.getApplyNo() + "-" + itemDto.getItNo());
                } else {
                    mergeNos.append("," + itemDto.getApplyNo() + "-" + itemDto.getItemNo());
                }
            }
            if (StringUtils.isNotBlank(mergeNos.toString())) {
                return mergeNos.substring(1);
            }
        }
        return "";
    }

    private ResponseDto resultToNode(ExamApplyDto applyDto, String terminalType, String application) {
        return resultToNode(applyDto, terminalType, application, "");
    }

    private ResponseDto resultToNode(ExamApplyDto applyDto, String terminalType, String application, String deviceId) {
        String reminderType = "2".equals(applyDto.getScheduleStatus()) ? "SCHEDULE" : "SIGN";
        if (StringUtils.isBlank(applyDto.getMergeNo())) {
            StringBuffer applyNos = new StringBuffer();
            List<ExamApplyDto> applyDtoList = examApplyMapper.findByApplyNo(applyDto);
            if (applyDtoList != null && !applyDtoList.isEmpty()) {
                for (ExamApplyDto examApply : applyDtoList) {
                    applyNos.append("," + examApply.getApplyNo() + "-" + examApply.getItemNo());
                }
            }
            applyDto.setMergeNo(applyNos.substring(1));
        }
        applyDto.setType(reminderType);
        applyDto.setTerminalType(terminalType);
        JsonResult jsonResult = examApplyPatientService.getExamApplyInfo(applyDto);
        ExamApplyDto examApplyInfo = (ExamApplyDto) jsonResult.getResult();
        ResponseDto responseDto = new ResponseDto();
        BeanUtils.copyProperties(examApplyInfo, responseDto);
        responseDto.setCanSchedule("1");
        responseDto.setScheduleStatus(StringUtils.isNotBlank(examApplyInfo.getScheduleStatus()) ? examApplyInfo.getScheduleStatus() : "0");
        if (StringUtils.isNotBlank(examApplyInfo.getExamStatus())) {
            if ("10".equals(examApplyInfo.getExamStatus())) {
                responseDto.setScheduleStatus("3");
            } else {
                responseDto.setScheduleStatus("4");
            }
        }
        responseDto.setScheduleStatus(StringUtils.isNotBlank(examApplyInfo.getExamStatus()) ? ("10".equals(examApplyInfo.getExamStatus()) ? "3" : responseDto.getScheduleStatus()) : responseDto.getScheduleStatus());
        ResponseDto.ScheduleHospital scheduleHospital = new ResponseDto.ScheduleHospital();
        scheduleHospital.setCode(examApplyInfo.getScheduleHospital());
        scheduleHospital.setName(examApplyInfo.getScheduleHospitalName());
        scheduleHospital.setLongitude(examApplyInfo.getLongitude());
        scheduleHospital.setLatitude(examApplyInfo.getLatitude());
        responseDto.setScheduleHospital(scheduleHospital);
        responseDto.setScheduleHospitalName(examApplyInfo.getScheduleHospitalName());
        if ("2".equals(responseDto.getScheduleStatus())) {
            responseDto.setQueueName(examApplyInfo.getQueueName());
            responseDto.setLocation(examApplyInfo.getSignLocation());
            responseDto.setZoneName(examApplyInfo.getSignZoneName());
        } else {
            responseDto.setQueueName(examApplyInfo.getExamQueueName());
            responseDto.setLocation(examApplyInfo.getLocation());
            responseDto.setZoneName(examApplyInfo.getZoneName());
            responseDto.setExamQueue(examApplyInfo.getExamQueueName());
            responseDto.setExamApm(examApplyInfo.getExamApm());
        }
        DictExamQueueDto deqDto = new DictExamQueueDto();
        deqDto.setExamClass(responseDto.getExamClass());
        deqDto.setQueueName(responseDto.getQueueName());
        String queueAlias = dictExamQueueService.getQueueAlias(deqDto);
        responseDto.setQueueName(queueAlias);
        responseDto.setWaitCount(applyDto.getWaitCount());
        responseDto.setExamNo(applyDto.getExamNo());
        responseDto.setExamItem(examApplyInfo.getExamItems());
        responseDto.setNotice(examApplyInfo.getNotices());
        responseDto.setReminder(examApplyInfo.getReminders());
        responseDto.setMergeNo(applyDto.getMergeNo());
        responseDto.setTimeEarliest(StringUtils.isNotBlank(applyDto.getTimeEarliest()) ? applyDto.getTimeEarliest() : examApplyInfo.getTimeEarliest());
        responseDto.setTimeSignin(StringUtils.isNotBlank(applyDto.getTimeSignin()) ? applyDto.getTimeSignin() : examApplyInfo.getTimeSignin());
        if (StringUtils.isNotBlank(applyDto.getExamNo())) {
            String cloudFilmUrl = "";
            String no = CommonUtil.toDecimal(Long.parseLong("1" + applyDto.getExamNo()));
            String cloudFilmUrl_tmp = configService.getConfigByName(ConstantsUtil.ConfigName.CLOUD_FILM_URL.getKey());
            if (StringUtils.isNotBlank(cloudFilmUrl_tmp)) {
                String inPatientNo = StringUtils.isNotBlank(applyDto.getInpatientNo()) ? applyDto.getInpatientNo() : "";
                String outPatientNo = StringUtils.isNotBlank(applyDto.getOutpatientNo()) ? applyDto.getOutpatientNo() : "";
                cloudFilmUrl = cloudFilmUrl_tmp.replace("#NO", no).replace("#INPATIENT_NO", inPatientNo).replace("#OUTPATIENT_NO", outPatientNo);
            }
            if (StringUtils.isBlank(application)) {
                cloudFilmUrl = "data:image/png;base64," + QrCodeUtil.creatRrCode(cloudFilmUrl, 150, 150);
            }
            responseDto.setCloudFilmUrl(cloudFilmUrl);
        }
        String isSign = applyDto.getIsSign();
        if (ConstantsUtil.TerminalType.SELF.getType().equals(terminalType)) {
            if (StringUtils.isNotBlank(deviceId)) {
                DictTerminalDto dictTerminalDto = new DictTerminalDto();
                dictTerminalDto.setIpAddress(deviceId);
                DictTerminalDto terminalDto = dictTerminalService.getSelfDeviceInfo(dictTerminalDto);
                if (terminalDto != null) {
                    List<String> unSignQueueList = CommonUtil.split2List(terminalDto.getUnSignQueue());
                    if (unSignQueueList.contains(responseDto.getQueueName())) {
                        isSign = "0";
                    }
                }
            }
            List<String> repeatSignByExamClassList = configService.getConfigListByName(ConstantsUtil.ConfigName.REPEAT_SIGN_BY_EXAM_CLASS.getKey());
            responseDto.setIsRepeatSign("0");
            if ("3".equals(responseDto.getScheduleStatus()) && !repeatSignByExamClassList.isEmpty() && repeatSignByExamClassList.contains(responseDto.getExamClass())) {
                responseDto.setIsRepeatSign("1");
            }
            if (StringUtils.isNotBlank(responseDto.getScheduleStatus())) {
                if (Integer.parseInt(responseDto.getScheduleStatus()) >= 3) {
                    List<String> patLocalIds = new ArrayList<String>();
                    List<String> examNos = new ArrayList<String>();
                    ExamScheduleItem examScheduleItem = new ExamScheduleItem();
                    examScheduleItem.setScheduledId(responseDto.getScheduledId());
                    List<ExamScheduleItem> esiList = examScheduleItemMapper.getExamScheduleItem(examScheduleItem);
                    if (esiList != null && !esiList.isEmpty()) {
                        for (ExamScheduleItem esi : esiList) {
                            if (StringUtils.isNotBlank(esi.getPatLocalId()) && !patLocalIds.contains(esi.getPatLocalId())) {
                                patLocalIds.add(esi.getPatLocalId());
                            }
                            if (StringUtils.isNotBlank(esi.getExamNo()) && !examNos.contains(esi.getExamNo())) {
                                examNos.add(esi.getExamNo());
                            }
                        }
                    }
                    responseDto.setPatLocalIds((List) patLocalIds);
                    responseDto.setExamNos((List) examNos);
                }
                if (Integer.parseInt(responseDto.getScheduleStatus()) == 2) {
                    ExamScheduleActDto actDto = new ExamScheduleActDto();
                    actDto.setScheduleId(responseDto.getScheduledId());
                    actDto.setTerminalType(ConstantsUtil.TerminalType.SELF.getType());
                    actDto.setActType("8");
                    int selfPrintCnt = examScheduleActService.findCount(actDto);
                    responseDto.setSelfPrintCnt(String.valueOf(selfPrintCnt));
                }
            }
        }
        responseDto.setIsSign(isSign);
        if (StringUtils.isNotBlank(applyDto.getItemTaboo())) {
            responseDto.setItemTaboo(applyDto.getItemTaboo());
        } else {
            responseDto.setItemTaboo(examApplyInfo.getItemTaboo());
        }
        if (examApplyInfo.getApplyNos() != null && !examApplyInfo.getApplyNos().isEmpty()) {
            ExamApplyOrganDto examApplyOrganDto = new ExamApplyOrganDto();
            examApplyOrganDto.setApplyNoList(examApplyInfo.getApplyNos());
            List<ExamApplyOrganDto> examApplyOrganList = examApplyOrganService.selectList(examApplyOrganDto);
            ArrayList<ExamApplyOrgan> examApplyOrgans = new ArrayList<>();
            examApplyOrganList.stream().forEach(vo -> {
                ExamApplyOrgan examApplyOrgan = new ExamApplyOrgan();
                BeanUtils.copyProperties(vo, examApplyOrgan);
                examApplyOrgans.add(examApplyOrgan);
            });
            responseDto.setExamOrgan((examApplyOrganList != null && !examApplyOrganList.isEmpty()) ? examApplyOrgans : new ArrayList<ExamApplyOrgan>());
        }
        return responseDto;
    }

    /**
     * 将自助机传过来的患者认证参数转换成查询条件的实体参数值
     *
     * @param dto
     * @param terminalDto
     */
    private void convertCardType4Self(ExamApplyDto dto, DictTerminalDto terminalDto) {
        if (StringUtils.isNotBlank(dto.getIcCard())) {
            String icCard = dto.getIcCard();
            dto.setCommonCard(icCard);
            dto.setIcCard((String) null);
            dto.setSickId((String) null);
            if (StringUtils.isNotBlank(terminalDto.getIcCardInputSupportType())) {
                String icCardInputSupportType = terminalDto.getIcCardInputSupportType();
                if ("common_card".equals(icCardInputSupportType)) {
                    dto.setCommonCard(icCard);
                } else if ("sick_id".equals(icCardInputSupportType)) {
                    dto.setSickId(icCard);
                    dto.setCommonCard((String) null);
                } else if ("ic_card".equals(icCardInputSupportType)) {
                    dto.setIcCard(icCard);
                    dto.setCommonCard((String) null);
                } else if ("inpatient_no".equals(icCardInputSupportType)) {
                    dto.setInpatientNo(icCard);
                } else if ("outpatient_no".equals(icCardInputSupportType)) {
                    dto.setOutpatientNo(icCard);
                } else if ("apply_no".equals(icCardInputSupportType)) {
                    dto.setApplyNo(icCard);
                } else if ("identity_card".equals(icCardInputSupportType)) {
                    dto.setIdentityCard(icCard);
                } else if ("mi_card".equals(icCardInputSupportType)) {
                    dto.setMiCard(icCard);
                } else if ("health_card".equals(icCardInputSupportType)) {
                    dto.setHealthCard(icCard);
                }
            }
        } else {
            dto.setIcCard(null);
        }
    }


    public RequestParamDto requestHandle(String req, boolean validheader) {
        RequestParamDto handle = new RequestParamDto();

        req = JaxbXmlUtil.delXmlHead(req);
        if (StringUtils.trimToNull(req) == null) {
            handle.setErrMsg("请求内容不能为空");
        } else {
            RequestParamDto requestParam = (RequestParamDto) marshallerUtils.parseXmlDataObject(RequestParamDto.class, req);
            if (requestParam != null) {
                if (validheader) {

                    RequestParamHeaderDto header = requestParam.getHeader();
                    if (header != null) {
                        if (StringUtils.isBlank(header.getTerminalType())) {
                            handle.setErrMsg("终端类型不能为空");
                        } else if (ConstantsUtil.TerminalType.SELF.getType().equals(header.getTerminalType()) &&
                                StringUtils.isBlank(header.getDeviceId())) {
                            handle.setErrMsg("设备ID不能为空");
                        } else {
                            requestParam.setTerminalType(header.getTerminalType());
                            requestParam.setDeviceId(header.getDeviceId());
                            requestParam.setUserId(header.getUserId());
                            requestParam.setUserName(header.getUserName());
                            requestParam.setAppType(header.getApplication());
                            BeanUtils.copyProperties(requestParam, handle);
                        }
                    } else {
                        handle.setErrMsg("请求头信息不能为空");
                    }
                }
            } else {

                handle.setErrMsg("请求内容不能为空");
            }
        }
        return handle;
    }
}
